from route import *
from loadOsm import *
from distance import *
from near_by_road import *
import android, string, time
from travel import *
############################################
droid = android.Android()

droid.ttsSpeak('Getting your location. Please wait.')
droid.startLocating()
droid.dialogCreateSpinnerProgress('GPS', 'Getting location...') 
droid.dialogShow()
time.sleep(20)
droid.dialogDismiss()
location = droid.readLocation().result
droid.stopLocating()
source=(location['gps']['latitude'],location['gps']['longitude'])
#source=(8.4383583068847656,76.992096304893494)
data = LoadOsm("iise.osm")
node1=near_by_road(float(source[0]),float(source[1]))
node1=int(node1)
print node1
node=[]
name=[]
lat=[]
lon=[]
poiname=[]

for line in open("latloniise.b","r"):
    line=line.split(";")
    node.append(line[0])
    name.append(line[1])
    lat.append(line[2])
    lon.append(line[3])
    l=line[4]
    poiname.append(l[:-1])
poiselectname=[]
poilatlon=[]    
poi=raw_input("enter your place of interest:")
for i in range(len(poiname)):
    if poi==poiname[i]:
       a=(lat[i],lon[i])
       print i
       poilatlon.append(a)
       poiselectname.append(name[i])

#print poiselectname
nearest=displacement(source,poilatlon)
print "Nearest "+poi+"s "+"are:"
for i in nearest:
    print poiselectname[i]
destination=raw_input("Enter the name of the place:")
sellat,sellon=lat[name.index(destination)],lon[name.index(destination)]
node2=near_by_road(float(sellat),float(sellon))
node2=int(node2)    
transport="cycle"
router = Router(data)
result, route = router.doRouteAsLL(node1, node2, transport)
f=open("track","w")
distlat=[]
distlon=[]
for i in route:
    distlat.append(float(i[0]))
    distlon.append(float(i[1]))
    line=str(i[0])+';'+str(i[1])
    f.write(line)
    f.write("\n")
f.close()
dist=distance(distlat,distlon)
print "The place is " + str(round(dist,1)) + "kilometres away from you"   

travel(route)

#if result == 'success':
#    print "Route: %s\n" % ",".join("%1.4f,%1.4f\n" % (i[0],i[1]) for i in route)
#else:
#    print "Failed (%s)" % result

